package com.hnkywl.sts.listener;

import com.hnkywl.sts.interceptor.AuthInterceptor;
import com.hnkywl.sts.service.xtgl.CzrzService;
import com.hnkywl.sts.service.xtgl.DlrzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-8
 * Time: 下午9:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class OnlineInfo {
    public static Map<String, UserInfo> userInfos = new HashMap<String, UserInfo>();
    public static Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();
    private DlrzService dlrzService;
    private CzrzService czrzService;

    public void addSession(String sessionId,HttpSession session) {
        sessions.put(sessionId,session);
    }

    public HttpSession delSession(String sessionId) {
        return sessions.remove(sessionId);
    }

    @Autowired
    public void setDlrzService(DlrzService dlrzService) {
        this.dlrzService = dlrzService;
    }

    @Autowired
    public void setCzrzService(CzrzService czrzService) {
        this.czrzService = czrzService;
    }

    //增加在线人员
    public void add(String sessionId, UserInfo userInfo) {
        //判断当前用户是否在原来的在线人中存在，如果存在即踢掉
        for (Map.Entry<String,UserInfo> userInfoTemp: userInfos.entrySet()) {
            if (userInfoTemp.getValue().getUserId().equals(userInfo.getUserId())) {
                userInfos.remove(userInfoTemp.getKey());
                //把session失效
                sessionInvalidate(userInfoTemp.getKey());
                break;
            }
        }

        userInfos.put(sessionId, userInfo);
        dlrzService.userLogin(userInfo);
    }

    private void sessionInvalidate(String sessionId) {
        HttpSession session = sessions.remove(sessionId);
        if (session != null) {
            session.invalidate();
        }
    }

    //删除在线人员
    public void remove(String sessionId) {
        UserInfo removeUserInfo =userInfos.remove(sessionId);
        if (removeUserInfo != null) {
            dlrzService.userLogout(removeUserInfo.getUserId(), removeUserInfo.getRemoteIP(),removeUserInfo.getMacAddress());
        }
    }

    //判断当前的Session是否合法
    public static boolean existsSession(HttpSession session) {
        if (userInfos.containsKey(session.getId())) {
            return true;
        } else {
            session.removeAttribute(AuthInterceptor.USER_SESSION_KEY);
            return false;
        }
    }

    public void log(HttpServletRequest request) {
        czrzService.operateLog(request);
    }

    public static int getUserCount() {
        return userInfos.size();
    }

    public static Map<String, UserInfo> getUserInfos() {
        return userInfos;
    }
}
